

using System;
using System.Linq;
using System.Linq.Dynamic.Core;
using Abp.Linq.Extensions;
using System.Collections.Generic;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using Dyan.PlatformSystem.Customers.Exporting;
using Dyan.PlatformSystem.Customers.Dtos;
using Dyan.PlatformSystem.Dto;
using Abp.Application.Services.Dto;
using Dyan.PlatformSystem.Authorization;
using Abp.Extensions;
using Abp.Authorization;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;

namespace Dyan.PlatformSystem.Customers
{
    public class CustomerAccountsAppAppService : PlatformSystemAppServiceBase, ICustomerAccountsAppAppService
    {
        private readonly IRepository<CustomerAccount, Guid> _customerAccountRepository;
        private readonly ICustomerAccountsExcelExporter _customerAccountsExcelExporter;


        public CustomerAccountsAppAppService(IRepository<CustomerAccount, Guid> customerAccountRepository, ICustomerAccountsExcelExporter customerAccountsExcelExporter)
        {
            _customerAccountRepository = customerAccountRepository;
            _customerAccountsExcelExporter = customerAccountsExcelExporter;

        }

        [ApiExplorerSettings(GroupName = "app")]
        public async Task<PagedResultDto<GetCustomerAccountForViewDto>> GetAll(GetAllCustomerAccountsInput input)
        {

            var filteredCustomerAccounts = _customerAccountRepository.GetAll()
                        .WhereIf(!string.IsNullOrWhiteSpace(input.Filter), e => false || e.Name.Contains(input.Filter) || e.Email.Contains(input.Filter) || e.PhoneNum.Contains(input.Filter) || e.PassWord.Contains(input.Filter))
                        .WhereIf(!string.IsNullOrWhiteSpace(input.NameFilter), e => e.Name.ToLower() == input.NameFilter.ToLower().Trim())
                        .WhereIf(!string.IsNullOrWhiteSpace(input.EmailFilter), e => e.Email.ToLower() == input.EmailFilter.ToLower().Trim())
                        .WhereIf(!string.IsNullOrWhiteSpace(input.PhoneNumFilter), e => e.PhoneNum.ToLower() == input.PhoneNumFilter.ToLower().Trim())
                        .WhereIf(!string.IsNullOrWhiteSpace(input.PassWordFilter), e => e.PassWord.ToLower() == input.PassWordFilter.ToLower().Trim());

            var pagedAndFilteredCustomerAccounts = filteredCustomerAccounts
                .OrderBy(input.Sorting ?? "id asc")
                .PageBy(input);

            var customerAccounts = from o in pagedAndFilteredCustomerAccounts
                                   select new GetCustomerAccountForViewDto()
                                   {
                                       CustomerAccount = new CustomerAccountDto
                                       {
                                           Name = o.Name,
                                           Email = o.Email,
                                           PhoneNum = o.PhoneNum,
                                           PassWord = o.PassWord,
                                           Id = o.Id
                                       }
                                   };

            var totalCount = await filteredCustomerAccounts.CountAsync();

            return new PagedResultDto<GetCustomerAccountForViewDto>(
                totalCount,
                await customerAccounts.ToListAsync()
            );
        }

        [ApiExplorerSettings(GroupName = "app")]
        public async Task CreateOrEdit(CreateOrEditCustomerAccountDto input)
        {
            if (input.Name == "editpwd")
            {
                var customerEditPwd = await _customerAccountRepository.FirstOrDefaultAsync(p => p.PhoneNum == input.PhoneNum);

                customerEditPwd.PassWord = input.PassWord;
                await _customerAccountRepository.UpdateAsync(customerEditPwd);
            }
            else
            {
                var customerAccount = ObjectMapper.Map<CustomerAccount>(input);
                await _customerAccountRepository.InsertAsync(customerAccount);
            }
        }
    }
}